package com.guigu.qingcheng.servicegoods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.qingcheng.pojo.goods.TbSpec;
import com.guigu.qingcheng.pojo.goods.TbTemplate;
import com.guigu.qingcheng.servicegoods.mapper.TbSpecMapper;
import com.guigu.qingcheng.servicegoods.service.TbSpecService;
import com.guigu.qingcheng.servicegoods.service.TbTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author testjava
 * @since 2020-07-08
 */
@Service
public class TbSpecServiceImpl extends ServiceImpl<TbSpecMapper, TbSpec> implements TbSpecService {
    @Resource
    private TbSpecMapper mapper;

    @Autowired
    private TbSpecService tbSpecService;

    @Autowired
    private TbTemplateService templateService;

    //根据条件查询封装查询对象
    public QueryWrapper<TbSpec> queryWrapper(Map<String, Object> searchMap){
        QueryWrapper<TbSpec> wapper = new QueryWrapper<TbSpec>();
        if(searchMap != null){
            if(searchMap.get("id") != null && !"".equals(searchMap.get("id")))
                wapper.eq("id", searchMap.get("id"));
            if(searchMap.get("name") != null && !"".equals(searchMap.get("name")))
                wapper.like("name", searchMap.get("name"));
            if(searchMap.get("options") != null && !"".equals(searchMap.get("options")))
                wapper.like("options", searchMap.get("options"));
            if(searchMap.get("seq") != null && !"".equals(searchMap.get("seq")))
                wapper.eq("seq", searchMap.get("seq"));
            if(searchMap.get("templateId") != null && !"".equals(searchMap.get("templateId")))
                wapper.eq("template_id", searchMap.get("templateId"));
        }
        return wapper;
    }

    @Override
    public Page<TbSpec> findPage(Map<String, Object> searchMap, int page, int size) {
        Page<TbSpec> pageTbPref = new Page<TbSpec>(page, size);
        QueryWrapper<TbSpec> query = queryWrapper(searchMap);
        IPage<TbSpec> list = this.tbSpecService.page(pageTbPref, query);
        return pageTbPref;
    }

    @Override
    public List<TbSpec> findList(Map<String, Object> searchMap) {
        QueryWrapper<TbSpec> wrapper = queryWrapper(searchMap);
        List<TbSpec> list = this.tbSpecService.list(wrapper);
        return list;
    }

    @Transactional
    @Override
    public Boolean addSpec(TbSpec spec) {
        // 同时修改下对应模板的规格总数：+1
        boolean save = this.tbSpecService.save(spec);
        Integer templateId = spec.getTemplateId();
        TbTemplate template = this.templateService.getById(templateId);
        template.setSpecNum(template.getSpecNum() + 1);
        boolean flag = this.templateService.updateById(template);
        if (save == flag ==true)
            return true;
        return false;
    }

    @Transactional
    @Override
    public Boolean deleteSpec(Integer id) {
        // 同时修改下对应模板的规格总数：-1
        TbSpec spec = this.tbSpecService.getById(id);
        TbTemplate tbTemplate = this.templateService.getById(spec.getTemplateId());
        tbTemplate.setSpecNum(tbTemplate.getSpecNum() - 1);
        boolean b1 = this.templateService.updateById(tbTemplate);
        boolean b = this.tbSpecService.removeById(id);
        if (b == b1 ==true)
            return true;
        return false;
    }
}
